<?php
	/*
	file ini digunakan untuk mengontrol login apakah user telah sesuai prosedur masuk aplikasi
	*/
class c_Login extends CI_Controller {

	function __construct(){
		parent::__construct();
		session_start();
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$this->load->model('m_login');

		$this->form_validation->set_message('required', '%s harus dimasukkan');
	}//end Login

	function index(){
            $this->home();

	}//end index

	function home(){
		//$data['title_page'] = 'Login';
		//$data['content_page'] = $this->load->view('admin/v_login',null,true);
		$this->load->view('admin/v_login');
	}//end index

	function user_login(){
		$login_username=$this->input->post('username');
		$login_password=$this->input->post('password');
		$result=$this->m_login->user_login($login_username, md5($login_password));
		if($result){
			echo "1";
		}else{
			echo "0";
		}

	}

	function cek_login(){
		$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
		$this->form_validation->set_rules('password', 'Password', 'trim|required|md5');

		if ($this->form_validation->run() == FALSE){
			redirect('c_login');
		}else{
			$this->session->sess_destroy();
			$valid = false;
			$paramdata = $_POST;
			$data_submit['username'] = $paramdata['username'];
			$data_submit['password'] = $paramdata['password'];
			$data = $this->m_login->check_login($data_submit);
			if ( $data != '' ){
				extract($data);
				$valid = true;

				$newdata = array(
					SESSION_USERID => $data_submit['username'],
					SESSION_GROUPNAMA => $GROUP_NAME
				);

				$this->session->set_userdata($newdata);

				$_SESSION[SESSION_USERID]=$data_submit['username'];
				$_SESSION[SESSION_GROUPNAMA]=$GROUP_NAME;

			}

			if($valid){
				redirect(base_url());
			}
			else{
				$data_view['msg']='Username yang dimasukkan salah';
				$data['title_page'] = 'Login';
				$data['content_page'] = $this->load->view('v_login',$data_view,true);
				$this->load->view('v_main',$data);
			}
		}
	}

	function logout(){
		$this->session->sess_destroy();
		$_SESSION[SESSION_USERID]="";
		$_SESSION[SESSION_GROUPNAMA]="";
		session_destroy();
		redirect('','location');
	}
}
?>
